package com.lyd.management.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lyd.management.mapper.UserMapper;
import com.lyd.management.pojo.User;
import com.lyd.management.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;

@Service
@Transactional
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Override
    public User login(String username, String password) {
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        //加密后的密码和数据库里的密码做对比
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername, username);
        queryWrapper.eq(User::getPassword, password);
        return userMapper.selectOne(queryWrapper);
    }

    //注册
    @Override

    public boolean register(User user) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername,user.getUsername());
        int count = userMapper.selectCount(queryWrapper);
        if(count>=1){
        //找到了存在的用户，返回用户已存在
            return false;
        }
       else{
        //用户密码加密,MD5加密
        user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
        // 若插入成功
        if (userMapper.insert(user)>0) {
            // 重新根据用户名和密码查找该用户
            LambdaQueryWrapper<User> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(User::getUsername, user.getUsername());
            queryWrapper1.eq(User::getPassword, user.getPassword());
            //u等于根据此用户和密码查询出来的这条用户数据
            User u = userMapper.selectOne(queryWrapper1);
            // 修改该用户的创建、更新者和时间
            u.setCreateUser(u.getId());
            u.setUpdateUser(u.getId());
            u.setCreateTime(LocalDateTime.now());
            u.setUpdateTime(LocalDateTime.now());
            // 更新数据库表中的数据
            userMapper.updateById(u);
            return true;
        }else{
            return false;
        }
    }
    }


    //修改密码
    @Override
    public boolean modifyPassword(String operatorId, String targetId, String oldPassword, String newPassword) {

        User user = userMapper.selectById(targetId);
        oldPassword = DigestUtils.md5DigestAsHex(oldPassword.getBytes());
        if(user.getPassword().equals(oldPassword)){
            newPassword = DigestUtils.md5DigestAsHex(newPassword.getBytes());
            //查找到对应用户，密码正确
            user.setPassword(newPassword);
            user.setUpdateUser(operatorId);
            user.setUpdateTime(LocalDateTime.now());
            userMapper.updateById(user);
            return true;
        }else{
            return false;
        }
    }


}